﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Composite.Data;
using System.Web.Script.Serialization;
using LCA.Common.Extensions;

public partial class Admin_LCASystem_Controls_TransportationB : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void RefreshComponents(Guid? SystemId)
    {
        ctlTransportBModal.RefreshComponents();

        using (DataConnection conn = new DataConnection())
        {
            if (!SystemId.HasValue)
                return;

            var list = from tb in conn.Get<LCA.Data.LCASystemTransportB>()
                       join v in conn.Get<LCA.Data.HeavyVehicle>() on tb.Vehicle equals v.Id
                       join u in conn.Get<LCA.Data.Unit>() on tb.DistanceUnit equals u.Id into joined
                       from j in joined.DefaultIfEmpty()
                       join uu in conn.Get<LCA.Data.Unit>() on tb.kgLoadUnit equals uu.Id into joined1
                       from jj in joined1.DefaultIfEmpty()
                       where tb.RelatedSystem == SystemId.Value
                       orderby v.Name
                       select new
                       {
                           vehicle_id = v.Id,
                           vehicle_name = v.Name + " - " + v.FullLoad + " ton "+v.PercentLoad+" % Loading",
                           distance = tb.Distance,
                           unit_distance_id = tb.DistanceUnit,
                           unit_distance_name = j.Name,
                           kg_load = tb.kgLoad,
                           unit_kgload_id = tb.kgLoadUnit,
                           unit_kgload_name = jj.Name
                       };

            txtTransportB.Text = new JavaScriptSerializer().Serialize(list.ToList());
        }
    }

    public void SaveData(Guid SystemId)
    {
        var table = new JavaScriptSerializer().Deserialize<dynamic>(txtTransportB.Text);

        using (DataConnection connection = new DataConnection())
        {
            var deletedOutputList = from i in connection.Get<LCA.Data.LCASystemTransportB>()
                                    where i.RelatedSystem == SystemId
                                    select i;

            foreach (LCA.Data.LCASystemTransportB item in deletedOutputList)
            {
                connection.Delete(item);
            }

            if (table != null)
            {
                LCA.Data.LCASystemTransportB tb = null;
                foreach (var row in table)
                {
                    string vehicle_id = row["vehicle_id"].ToString();
                    string distance = row["distance"].ToString();
                    string unit_distance_id = row["unit_distance_id"];
                    string kg_load = row["kg_load"].ToString();
                    string unit_kgload_id = row["unit_kgload_id"];

                    tb = connection.CreateNew<LCA.Data.LCASystemTransportB>();

                    tb.Vehicle = vehicle_id.ToGuid().Value;
                    tb.Distance = distance.ToString();
                    tb.DistanceUnit = unit_distance_id.ToGuid();
                    tb.kgLoad = kg_load.ToString();
                    tb.kgLoadUnit = unit_kgload_id.ToGuid();
                    tb.RelatedSystem = SystemId;

                    connection.Add(tb);
                }
            }
        }
    }
}